﻿using ClassLibrary1;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using TextBox = System.Web.UI.WebControls.TextBox;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillDate();
            }


        }
        private void FillDate()
        {
            string Select = "select Id,ProductsName,BrandId,Describe,Remarks from Products";
            var dt = Dbhelper.GetDataTable(Select);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }




        ///查询
        protected void Button1_Click(object sender, EventArgs e)
        {
            string Select = string.Format("select * from Products  where ProductsName like '%{0}%' or Describe like '%{1}%' or Remarks like '%{2}%'", TextBox1.Text, TextBox1.Text, TextBox1.Text);
            var dt = Dbhelper.GetDataTable(Select);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }


        //进入编辑模式
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillDate();
        }


        //取消编辑
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillDate();
        }

        //封装数据
        private string GetValues(int RowIndex, int ColIndex)
        {
            var control = GridView1.Rows[RowIndex].Cells[ColIndex];
            var res = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return res;
        }

        //更新数据
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {

                var IdString = GetValues(e.RowIndex, 0);
                var IsOk = int.TryParse(IdString, out int Id);

                var ProductsName = GetValues(e.RowIndex, 1);
                var BrandId = GetValues(e.RowIndex, 2);
                var Describe = GetValues(e.RowIndex, 3);
                var Remarks = GetValues(e.RowIndex, 4);

                if (IsOk)
                {
                    //Id存在 直接更新
                    var sql = string.Format("update Products set ProductsName='{0}',BrandId='{1}',Describe='{2}',Remarks='{3}' where Id={4}", ProductsName,BrandId,Describe,Remarks,Id);

                    var number = Dbhelper.AddOrUpdateOrDelete(sql);
                    if (number > 0)
                    {
                        Response.Write("更新成功");

                        //退出编辑模式
                        GridView1.EditIndex = -1;
                    }
                    else
                    {
                        Response.Write("更新失败");
                    }
                }
                else
                {
                    //Id不存在 添加数据
                    var sql = string.Format("insert into Products(ProductsName,BrandId,Describe,Remarks)values('{0}',{1},'{2}','{3}')",ProductsName,BrandId,Describe,Remarks);
                    var number = Dbhelper.AddOrUpdateOrDelete(sql);
                    if (number > 0)
                    {
                        Response.Write("添加成功");

                        //退出编辑模式
                        GridView1.EditIndex = -1;

                        FillDate();
                    }
                    else
                    {
                        Response.Write("添加失败");
                    }
                }



            }
            catch (Exception s)
            {
                s.Message.ToString();
            }
            finally
            {

                FillDate();
            }


        }

        //删除一行
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            try
            {
                var Id = GetValues(e.RowIndex, 0);
                var res = MessageBox.Show("确定要删除嘛？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals(DialogResult.Yes))
                {

                    var delete = string.Format("delete from Products where id={0}", Id);
                    var td = Dbhelper.AddOrUpdateOrDelete(delete);
                    if (td > 0)
                    {
                        Response.Write("删除成功");
                    }
                    else
                    {
                        Response.Write("删除失败");
                    }
                }
                else
                {
                    FillDate();
                }



            }
            catch (Exception s)
            {
                s.Message.ToString();
            }
            finally
            {
                FillDate();
            }


        }
        private DataTable Data
        {
            get
            {
                var sql = "select * from Products";
                var dt = Dbhelper.GetDataTable(sql);
                return dt;
            }
        }
        // 添加
        protected void Button2_Click(object sender, EventArgs e)
        {
            #region 遍历新行
            //var dt =new  DataTable();
            //dt.Columns.Add("Id");
            //dt.Columns.Add("ProductsName");
            //dt.Columns.Add("BrandId");
            //dt.Columns.Add("Describe");
            //dt.Columns.Add("Remarks");

            //foreach (GridViewRow row in GridView1.Rows)
            //{
            //    var tempRow = dt.NewRow();
            //    tempRow["Id"] = GetValues(row.RowIndex, 0);
            //    tempRow["ProductsName"] = GetValues(row.RowIndex,1);
            //    tempRow["BrandId"] = GetValues(row.RowIndex, 2);
            //    tempRow["Describe"] = GetValues(row.RowIndex, 3);
            //    tempRow["Remarks"] = GetValues(row.RowIndex, 4).Replace("&nbsp", "");

            //    //将新添加的行添加到dt表中
            //    dt.Rows.Add(tempRow);
            //}
            //在表中再添加一个新行
            #endregion

            var dt = Data;
            dt.Rows.Add(dt.NewRow());

            GridView1.DataSource = dt;
            GridView1.DataBind();

            //取消编辑
            GridView1.EditIndex = dt.Rows.Count - 1;

            //将dt表 放入GridView1
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }

       
    }
}